package com.jiutong.meipengdai.ui.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.entity.PwdCheckRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.present.UserInfoPresent;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 通过旧密码 重置登录密码
 * Created by suxi on 2017/6/2.
 */

public class PwdResetByOldPwdActivity extends BaseActivity implements RegisterAndLoginBiz {

    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_old_pwd)
    EditText et_old_pwd;
    @Bind(R.id.tv_phone_verify)
    TextView tv_phone_verify;
    @Bind(R.id.btn_next)
    Button btn_next;
    @Bind(R.id.tv_my_phone)
    TextView tv_my_phone;
    private UserInfoPresent userInfoPresent;
    private String phone;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_pwd_reset_byold;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText(R.string.reset_pwd);
        Bundle bundle = getIntent().getExtras();
        phone = bundle.getString("phone");
        tv_my_phone.setText(phone);
        btn_next.setEnabled(false);
        et_old_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(getPassword().length()>=8&&getPassword().length()<=16){
                    btn_next.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                    btn_next.setEnabled(true);
                }else{
                    btn_next.setBackgroundResource(R.drawable.shape_light_gray_btn);
                    btn_next.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(getPassword().length()>=8&&getPassword().length()<=16){
                    btn_next.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                    btn_next.setEnabled(true);
                }else{
                    btn_next.setBackgroundResource(R.drawable.shape_light_gray_btn);
                    btn_next.setEnabled(false);
                }
            }
        });

        userInfoPresent = new UserInfoPresent(this, this);
    }

    @OnClick({R.id.rl_back, R.id.btn_next, R.id.tv_phone_verify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_next:  //下一步
                if (checkInputContent()) {
                    userInfoPresent.checkPwd();
                }
                break;
            case R.id.tv_phone_verify:  //手机验证
                Bundle bundle1 = new Bundle();
                bundle1.putString("phone", phone);
                openActivity(PwdResetByMsgActivity.class, bundle1);
                break;
        }
    }

    private boolean checkInputContent() {
        if (TextUtils.isEmpty(getPassword())) {
            toastShortShow("请输入原始密码");
            return false;
        }

        return true;
    }

    @Override
    public String getPhoneNum() {
        return null;
    }

    @Override
    public String getPassword() {
        return et_old_pwd.getText().toString().trim();
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        PwdCheckRespond respond = (PwdCheckRespond) object;
        String success = respond.getData().getSuccess();
        if (success.equals("1")) {
            Bundle bundle = new Bundle();
            bundle.putString("pwd", getPassword());
            openActivity(ChangePwdActivity.class, bundle);
            finish();
        } else {
            toastShortShow("您输入的密码不正确，请重新输入！");
        }
    }

    @Override
    public void onError(Throwable e) {

    }
}
